package com.sservice.web.quartz;

import com.sservice.banfu.mapper.SalePersonMapper;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.model.dao.*;
import com.sservice.model.entity.Goods;
import com.sservice.model.entity.GoodsPriceIndex;
import com.sservice.banfu.e.SalePerson;
import com.sservice.model.vo.ClienteleVo;
import com.sservice.model.vo.GoodsVo;
import com.sservice.banfu.vo.SalePersonVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 价格指数定时任务 需满足幂等性
 * Created by byte2 on 2017-03-30.
 */
public class GoodsPriceIndexQuart {
    @Autowired
    private IGoodsDao goodsDao;
    @Autowired
    private IOfferDao offerDao;
    @Autowired
    private GoodsPriceIndexMapper gpiDao;
    @Autowired
    private SalePersonMapper salePersonMapper;
    @Autowired
    private SalePersonService1 salePersonService1;
    @Autowired
    private IClienteleDao clienteleDao;

    //到了某个时刻就会被调用
    public void autoRun() {
        System.out.println("价格指数定时任务 to run :" + new Date().toString());
//        GoodsVo gv = new GoodsVo();
//        gv.setSize(100000);
//
//        List<Goods> list = goodsDao.queryByPage(gv);
//
//
//        for (Goods g : list) {
//            GoodsPriceIndex gpi = new GoodsPriceIndex();
//            gpi.setGoodsId(String.valueOf(g.getId()));
//            gpi.setPrice(offerDao.queryOfferAvgPriceByGoodsId(String.valueOf(g.getId())));
//            GoodsPriceIndex gpi1 = new GoodsPriceIndex();
//            gpi1.setGoodsId(String.valueOf(g.getId()));
//            GoodsPriceIndex gpi2 = gpiDao.selectBy(gpi1);
//            if (gpi2 == null) {
//                gpiDao.insertSelective(gpi);
//            } else {
//                gpi.setId(gpi2.getId());
//                gpiDao.updateByPrimaryKey(gpi);
//            }
//
//        }
    }

    public void setGoodsCountZeroRun() {
        System.out.println("产品分类订单数量定时清0任务 to run :" + new Date().toString());
//        GoodsVo gv = new GoodsVo();
//        gv.setSize(100000);
//
//        List<Goods> list = goodsDao.queryByPage(gv);
//
//        for (Goods g : list) {
//            Goods g1 = new Goods();
//            g1.setId(g.getId());
//            g1.setCount(0);
//            goodsDao.updateByPrimaryKeySelective(g1);
//        }
    }

    /***
     * 校准销售员客户和销售员数量
     */
    public void correctSalePersonCount() {
        System.out.println("校准销售员客户和销售员数量定时任务 to run :" + new Date().toString());
        SalePerson gv = new SalePerson();
//        gv.setSize(100000);
        List<SalePerson> list = salePersonService1.queryByPage(gv);
        for (SalePerson g : list) {
            SalePerson g1 = new SalePerson();
            ClienteleVo cv = new ClienteleVo();
            cv.setSalesId(String.valueOf(g.getId()));
            //g1.setCustomerCount(clienteleDao.queryCount(cv));
            SalePersonVo spv = new SalePersonVo();
            spv.setUserId(String.valueOf(g.getId()));
            g1.setSalesCount(salePersonService1.queryCount(null));
            g1.setId(g.getId());
            salePersonMapper.updateByPrimaryKeySelective(g1);
        }
    }


    /***
     * 校正销售佣金比例
     */
    public void correctSalePersonCommission() {
        System.out.println("校准销售员客户和销售员数量定时任务 to run :" + new Date().toString());
        SalePerson gv = new SalePerson();
//        gv.setSize(100000);
//        gv.setLevel("1"); //销售经理
        List<SalePerson> list = salePersonService1.queryByPage(gv);
    }


}
